package Week10;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.io.IOException;
import java.net.URL;



public class polygonInOut {
	private ArrayList<Polygon> polygonListin, polygonListOut;


	public polygonInOut(ArrayList<Polygon> polygonListin) {
		this.polygonListin = polygonListin;
		this.polygonListOut = polygonListOut;
	}

	public ArrayList<Polygon> getPolygonListin() {
		return polygonListin;
	}

	public void setPolygonListin(ArrayList<Polygon> polygonListin) {
		this.polygonListin = polygonListin;

	}
	
	//Write array list of Polygons to a user-specified binary file.
	public void writeFile(ArrayList<Polygon> polygonListin, File selectedFile){
		try{
			FileOutputStream fos = new FileOutputStream(selectedFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(polygonListin);
			oos.flush();
			oos.close();
		}
		catch(IOException e){
			e.printStackTrace();
		}


	}
	
	//Read array list of Polygons from a user-specified binary file.
	public ArrayList<Polygon> readFile(File selectedFile){
		try{
			FileInputStream fis = new FileInputStream(selectedFile);
			ObjectInputStream ois = new ObjectInputStream(fis);
			polygonListOut = (ArrayList<Polygon>) ois.readObject();
			ois.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return polygonListOut;

	}
	
	//Read array list of Polygons from the web
	public ArrayList<Polygon> readFileWeb(){
		try{
			URL polygonUrl = new URL("https://sites.google.com/site/myintrotogis/home/my-file-cabinet/Week10-Polygons.bif");
			ObjectInputStream ois = new ObjectInputStream((InputStream) polygonUrl.getContent());
			polygonListOut = (ArrayList<Polygon>) ois.readObject();
			ois.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return polygonListOut;

	}
}